<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Plan
 *
 * @property int $id
 * @property string $title 标题
 * @property string $cover 封面
 * @property int $user_id 用户id
 * @property int $status 状态
 * @property string $description 简介
 * @property \Illuminate\Support\Carbon $date 拍摄时间
 * @property string $address 拍摄地点
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Photo[] $photoes
 * @property-read int|null $photoes_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $users
 * @property-read int|null $users_count
 * @method static \Illuminate\Database\Eloquent\Builder|Plan newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Plan newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Plan query()
 * @method static \Illuminate\Database\Eloquent\Builder|Plan whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Plan whereCover($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Plan whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Plan whereDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Plan whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Plan whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Plan whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Plan whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Plan whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Plan whereUserId($value)
 * @mixin \Eloquent
 */
class Plan extends Model
{
    protected $fillable = ['title', 'cover', 'user_id', 'status', 'description', 'date', 'address'];

    protected $casts = [
        'date' => 'datetime'
    ];

    public static $statusMap = [
        '1' => '未开始',
        '2' => '拍摄中',
        '3' => '待返片',
        '4' => '精修中',
        '5' => '已完成',
    ];

    public function photoes()
    {
        return $this->hasMany(Photo::class);
    }

    public function users()
    {
        return $this->belongsToMany(User::class)->withTimestamps();
    }

    public function coverUrl()
    {
        if (strpos($this->cover, 'http:') !== false || strpos($this->cover, 'https:') !== false) {
            return $this->cover;
        }
        return $this->cover ? url('storage/' . $this->cover) : url('images/avatar/plan.jpg');
    }
}
